#include "tim.h"
#include "led.h"
#include "display.h"

/**************************实现函数********************************************
*函数原型:
*功　　能:1ms中断一次,计数器为1000
//基础设置，时基和比较输出设置，由于这里只需定时，所以不用OC比较输?
            prescaler is 1200,that is 168000000/168/500=2000Hz;
*******************************************************************************/
void TIM6_Init(uint16_t period_num)
{
    NVIC_InitTypeDef NVIC_InitStructure;
    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6, ENABLE);

    TIM_DeInit(TIM6);

    TIM_TimeBaseStructure.TIM_Period = period_num; //装载值
    TIM_TimeBaseStructure.TIM_Prescaler = 72 - 1; //分频系数    1MHz
    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //or TIM_CKD_DIV2 or TIM_CKD_DIV4     不分频
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInit(TIM6, &TIM_TimeBaseStructure);
    TIM_ClearFlag(TIM6, TIM_FLAG_Update);       //clear the TIM2 overflow interrupt flag
    TIM_ITConfig(TIM6, TIM_IT_Update, ENABLE);      //TIM2 overflow interrupt enable
    TIM_Cmd(TIM6, ENABLE);

    //    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);   //TIM6

    NVIC_InitStructure.NVIC_IRQChannel = TIM6_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);

    //    NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
    //    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;     //抢占优先级
    //    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;            //响应优先级
    //    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    //    NVIC_Init(&NVIC_InitStructure);

}




